window.onload = function () {
    fullTable(emList);
}

/**
 * 展示表格
 * @param arry  展示表格的内容
 */
function fullTable(arry) {
    let str = "";
    arry.forEach(n => {
        str += `<tr><td>${n.code}</td><td>${n.name}</td><td>${n.moeny}</td><td>${n.sex}</td>
<td><input type="button" value="删除" onclick="del(${n.code});">
<input type="button" value="修改" onclick="showUpdateDiv(${n.code});"></td></tr>`;
    });
    $("data").innerHTML = str;
}

/**
 * 按id获取对象
 * @param id 获取对象的id
 * @returns {HTMLElement} 返回一个对象
 */
function $(id) {
    return document.getElementById(id);
}

/**
 * 按id删除
 * @param code id
 */
function del(code) {
    console.log(code);
    var index = emList.findIndex(n => n.code == code);
    console.log(index);
    emList.splice(index, 1);
    fullTable(emList);
}

/**
 * 按姓名查找
 */
function findByName() {
    var str = $("findName").value;
    var newList = emList.filter(n => (n.name.indexOf(str)) != -1);
    fullTable(newList);
}

/**
 * 显示增加
 */
function showAddDiv() {
    $("showDiv").style.visibility = "visible";
}
/**
 * 验证name
 * @returns {boolean}  返回是否验证成功
 */
function checkName() {
    return checkFormElement(/^[\u4e00-\u9fa5]{2,10}$/, 'nameInput', 'nameSpan');
}

function checkCount() {
    return checkFormElement(/^\d+$/, 'countInput', 'countSpan');
}
/**
 * 验证编号
 * @returns {boolean}  返回是否验证成功
 */
function checkCode() {
    var flag = checkFormElement(/^[\d]{1,5}$/, 'codeInput', 'codeSpan');
    var textCode = $("codeInput").value;
    var a = emList.findIndex(n => n.code == textCode);
    if (a == -1 & flag) {
        return true;
    } else {
        return false;
    }
}
/**
 * 验证sex信息
 * @returns {boolean}  返回是否验证成功
 */
function checkSex() {
    return checkFormElement(/^(男|女)$/, 'sexnput', 'sexSpan');
}

/**
 * 验证money信息
 * @returns {boolean}  返回是否验证成功
 */
function checkSetCount() {
    return checkFormElement(/^\d+$/, 'countSetInput', 'countSetSpan');
}

/**
 * 信息验证
 * @param rexp 验证的正则表达式
 * @param textId 文本框的id
 * @param spanId  文本框附带span标签的id
 * @returns {boolean} 返回是否验证成功
 */
function checkFormElement(rexp, textId, spanId) {
    var spanObj = $(spanId);
    if (rexp.test($(textId).value)) {
        spanObj.innerHTML = "√"
        spanObj.style.color = "green"
        spanObj.style.fontSize = "15px"
        return true;
    } else {
        spanObj.innerHTML = "×"
        spanObj.style.color = "red"
        spanObj.style.fontSize = "15px"
        return false;
    }
}

/**
 * 增加对象
 */
function add() {
    var obj = {
        code: $("codeInput").value,
        name: $("nameInput").value,
        moeny: $("countInput").value,
        sex: $("sexnput").value
    };
    emList.push(obj);
    closeDiv("showDiv");
    fullTable(emList);
}

/**
 * 关闭窗口
 * @param id 关闭窗口的id
 */
function closeDiv(id) {
    $(id).style.visibility = "hidden";
}

/**
 * 确定是否增加
 */
function sureAdd(){
    if (checkCode() & checkSex() & checkCount() & checkName()) {
        add();
    }
}
/**
 * 确定是否修改
 */
function sureUpdate(){
    if (checkSetCount()) {
        update()
    }
}
/**
 * 显示修改窗口
 * @param code 要修改信息的编号
 */
function showUpdateDiv(code) {
    $("updateDiv").style.visibility = "visible";
    var obj = emList.find(n => n.code == code);
    $("codeSetSpan").innerHTML = obj.code;
    $("nameSetSpan").innerHTML = obj.name;
    $("countSetInput").value = obj.moeny;
    $("sexSetSpan").innerHTML = obj.sex;
}

/**
 * 修改
 */
function update() {
    let code = $("codeSetSpan").innerHTML;
    console.log(code);
    let setobj = emList.find(n => n.code == code);
    setobj.moeny = $("countSetInput").value;
    fullTable(emList);
    closeDiv("updateDiv");
}

/**
 * 按照编号排序
 */
function forCode() {
    emList.sort((a, b) => a.code - b.code);
    fullTable(emList);
}

/**
 * 按照money排序
 */
function forMoney() {
    emList.sort((a, b) => b.moeny - a.moeny);
    fullTable(emList);
}